
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root ;

    public boolean search(int val) {
        TreeNode p = root;
        while (p != null) {
            if (p.val > val) {
                p = p.left;
            } else if (p.val < val) {
                p = p.right;
            }else {
                return true;
            }
        }
        return false;
    }

    public void insert(int val) {
        TreeNode p = root;
        TreeNode parent = null;
        while (p != null) {
            parent = p;
            if (p.val > val) {
                p = p.left;
            } else if (p.val < val) {
                p = p.right;
            }else {
                //不符合
                return;
            }
        }

        TreeNode node = new TreeNode(val);
        if (parent == null) {
            root = node;
        } else if (parent.val > val) {
            parent.left = node;
        }else {
            parent.right = node;
        }
    }
}